<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getCompanySummary } from '@/api/dashboard/smsk'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    checkDate: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      setTimeoutTimer: null,
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.setTimeoutTimer) {
        clearTimeout(this.setTimeoutTimer)
      }
      this.initChart()
    })
  },
  beforeDestroy() {
    if (this.setTimeoutTimer) {
      clearTimeout(this.setTimeoutTimer)
    }
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getTitle() {
      this.checkDate.substring(0, 4) + '-' + this.checkDate.substring(4, 6) + '-' + this.checkDate.substring(6, 8) + ' 按企业汇总'
    },
    initChart() {
      getCompanySummary(this.checkDate).then(response => {
        const data = { companyArray: [], entryAmountArray: [], checkAmountArray: [], gapAmountArray: [], inputAmountArray: [], counterfeitAmountArray: [] }
        if (response.data) {
          for (var i = 0; i < response.data.length; i++) {
            data.companyArray.push(response.data[i].companyName)
            data.entryAmountArray.push(response.data[i].entryAmount)
            data.checkAmountArray.push(response.data[i].checkAmount)
            data.gapAmountArray.push(response.data[i].gapAmount)
            data.inputAmountArray.push(response.data[i].inputAmount)
            data.counterfeitAmountArray.push(response.data[i].counterfeitAmount)
          }
        }
        this.chart = echarts.init(this.$el, 'macarons')
        const _this = this
        const companyData = response.data
        this.chart.on('click', function(params) {
          console.log(params)
          if (params && params.name) {
            for (var i = 0; i < companyData.length; i++) {
              if (companyData[i].companyName === params.name) {
                _this.$emit('company', { companyId: companyData[i].companyId, companyName: companyData[i].companyName })
                return
              }
            }
          }
        })
        this.setOptions(data)
        clearTimeout(this.setTimeoutTimer)
        this.setTimeoutTimer = setTimeout(this.initChart, 60 * 1000)
      })
    },
    setOptions({ companyArray, entryAmountArray, checkAmountArray, gapAmountArray, inputAmountArray, counterfeitAmountArray }) {
      this.chart.setOption({
        title: {
          text: '按企业汇总 ' + this.checkDate.substring(0, 4) + '-' + this.checkDate.substring(4, 6) + '-' + this.checkDate.substring(6, 8)
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'line' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: ['入账金额', '点钞金额', '差额', '录入金额', '假钞金额']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              show: false
            },
            data: companyArray
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '入账金额',
            type: 'bar',
            stack: '入账',
            label: {
              show: true,
              position: 'inside'
            },
            data: entryAmountArray
          },
          {
            name: '点钞金额',
            type: 'bar',
            stack: '清点',
            label: {
              show: true
            },
            data: checkAmountArray
          },
          {
            name: '差额',
            type: 'bar',
            stack: '清点',
            label: {
              show: true,
              position: 'inside'
            },
            data: gapAmountArray
          },
          {
            name: '录入金额',
            type: 'bar',
            stack: '客户',
            label: {
              show: true
            },
            data: inputAmountArray
          },
          {
            name: '假钞金额',
            type: 'bar',
            stack: '客户',
            label: {
              show: true,
              position: 'left'
            },
            data: counterfeitAmountArray
          }
        ]
      })
    }
  }
}
</script>
